package com.dz.syndata.service.impl;

import com.dz.syndata.bean.device.DeviceBean;
import com.dz.syndata.bean.entity.MobileSbyjData;
import com.dz.syndata.mapper.one.PrimaryDeviceMapper;
import com.dz.syndata.mapper.two.SecondaryUserMapper;
import com.dz.syndata.service.IDevice4DtzzService;
import com.dz.syndata.service.IDeviceInfoService;
import com.dz.syndata.service.impl.device.DeviceDataImpl;
import com.dz.syndata.util.HttpRequest;
import com.dz.syndata.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
public class Device4DtzzServiceImpl implements IDevice4DtzzService {
    public String url = "http://172.168.65.34:8082/kdmService/rest/1.0/kksData?method=getRTDataSnapshot&kksCodes=";

    @Autowired
    SecondaryUserMapper secondaryUserMapper;
    @Qualifier("redisR1Template")
    @Autowired
    RedisTemplate<String,Object> redisR1Template;
    @Qualifier("redisR2Template")
    @Autowired
    RedisTemplate<String,Object>redisR2Template;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<MobileSbyjData> selectDeviceData() {
        return secondaryUserMapper.findAll();
    }

    @Override
    public void updateSbyj01Data(String code, String resultValue) {
        secondaryUserMapper.update4DtzzDataValue1(code, resultValue);
    }

    @Override
    public void updateSbyj02Data(String code, String resultValue) {
        secondaryUserMapper.update4DtzzDataValue2(code, resultValue);

    }

    @Override
    public void updateSbyj03Data(String code, String resultValue) {
        secondaryUserMapper.update4DtzzDataValue3(code, resultValue);

    }

    @Override
    public void updateSbyj04Data(String code, String resultValue) {
        secondaryUserMapper.update4DtzzDataValue4(code, resultValue);

    }

    @Override
    public void updateSbyj05Data(String code, String resultValue) {
        secondaryUserMapper.update4DtzzDataValue5(code, resultValue);

    }

    @Override
    public void selectSbyjData() {
        ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
        execService.scheduleAtFixedRate(new Runnable() {
            public void run() {
                selectData();
                try {
                    for (int i = 0; i < 60; i++) {
                        Thread.sleep(1000L);
                    }
                    logger.info("Device4DtzzServiceImpl->>>执行完毕");
                    System.out.println("执行完毕^^^");
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error(e.toString());
                }


            }
        }, 5, 2, TimeUnit.SECONDS);
//        selectData();
    }
    public void selectData(){
        List<MobileSbyjData> mobileSbyjDatas = selectDeviceData();
        if(mobileSbyjDatas.size()!=0){
            for (MobileSbyjData mMobileSbyjData :
                    mobileSbyjDatas) {
                List<String> stringList = new ArrayList<>();
                String m1Code = mMobileSbyjData.getM1Code();
                String m2Code = mMobileSbyjData.getM2Code();
                String m3Code = mMobileSbyjData.getM3Code();
                String m4Code = mMobileSbyjData.getM4Code();
                String m5Code = mMobileSbyjData.getM5Code();
                stringList.add(m1Code);
                stringList.add(m2Code);
                stringList.add(m3Code);
                stringList.add(m4Code);
                stringList.add(m5Code);
//                logger.info("m1Code:"+m1Code+"m2Code:"+m2Code+"m3Code："
//                        +m3Code+"m4Code:"+m4Code+"m5Code:"+m5Code);
                try {
                    for (int i = 0; i < stringList.size(); i++) {
                        {
                            String code = stringList.get(i);
                            if( StringUtils.isNotNull(code)||StringUtils.isNotEmpty(code)){
                                String result = HttpRequest.sendGet(url + code, null);
                                if (!result.isEmpty()) {

                                    DeviceBean deviceBean1 = new Gson().fromJson(result, DeviceBean.class);
                                    if (deviceBean1.getBasicResponse().getSucceeded() == 1) {
                                        for (DeviceBean.RTDataSetsBean mRtDataSetsBean : deviceBean1.getRTDataSets()
                                        ) {
                                            if (mRtDataSetsBean.getTag().equals("OK")) {
                                                if(mRtDataSetsBean.getRTDataValues().size()!=0){
                                                for (DeviceBean.RTDataSetsBean.RTDataValuesBean mValuesBean : mRtDataSetsBean.getRTDataValues()) {
                                                    if(mValuesBean.getValue()!=-1){
                                                        try {
                                                            switch (i) {
                                                                case 0:
                                                                    secondaryUserMapper.update4DtzzDataValue1(stringList.get(0), mValuesBean.getValue()+"");
                                                                    redisR1Template.opsForValue().set(stringList.get(0),mValuesBean.getValue());
                                                                    redisR2Template.opsForValue().set(stringList.get(0),mValuesBean.getValue());
                                                                    break;
                                                                case 1:
                                                                    secondaryUserMapper.update4DtzzDataValue2(stringList.get(1), mValuesBean.getValue()+"");
                                                                    redisR1Template.opsForValue().set(stringList.get(1),mValuesBean.getValue());
                                                                    redisR2Template.opsForValue().set(stringList.get(1),mValuesBean.getValue());
                                                                    break;
                                                                case 2:
                                                                    secondaryUserMapper.update4DtzzDataValue3(stringList.get(2), mValuesBean.getValue()+"");
                                                                    redisR1Template.opsForValue().set(stringList.get(2),mValuesBean.getValue());
                                                                    redisR2Template.opsForValue().set(stringList.get(2),mValuesBean.getValue());
                                                                    break;
                                                                case 3:
                                                                    secondaryUserMapper.update4DtzzDataValue4(stringList.get(3), mValuesBean.getValue()+"");
                                                                    redisR1Template.opsForValue().set(stringList.get(3),mValuesBean.getValue());
                                                                    redisR2Template.opsForValue().set(stringList.get(3),mValuesBean.getValue());
                                                                    break;
                                                                case 4:
                                                                    secondaryUserMapper.update4DtzzDataValue5(stringList.get(4), mValuesBean.getValue()+"");
                                                                    redisR1Template.opsForValue().set(stringList.get(4),mValuesBean.getValue());
                                                                    redisR2Template.opsForValue().set(stringList.get(4),mValuesBean.getValue());
                                                                    break;
                                                            }
                                                        } catch (Exception e) {
                                                            e.printStackTrace();
                                                        } finally {
                                                            //手动释放连接
                                                            RedisConnectionUtils.unbindConnection(redisR1Template.getConnectionFactory());
                                                            RedisConnectionUtils.unbindConnection(redisR2Template.getConnectionFactory());

                                                        }

                                                    }
                                                }
                                                }
                                            }
                                        }
                                    }
                                }
                            }else{
                                System.out.println("编码为空--------------->");
                                logger.info("编码为空--------------->");
                                continue;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("异常信息："+e.toString()+"------------->");
                    logger.error(e.toString());
                }

            }

        }
    }

}
